package com.powernode.model;

import com.powernode.constant.BusinessEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author:huxiaochao
 * @Description:统一响应式结果
 * @DateTime:2025-03-07 15:46
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("配置统一响应式结果")
//响应式数据呢？不可以只有一个,因此我们要使用泛型，得出多个响应式数据的结果
public class Result<T> implements Serializable {
    //1.状态码
    @ApiModelProperty(value = "状态码")
    private Integer code;

    //2.响应信息
    @ApiModelProperty(value = "响应信息")
    private String message;

    //3.响应数据
    @ApiModelProperty(value = "响应数据")
    private T data;

    /**
     * 操作成功
     */
    public static <T> Result<T> success(T data) {
        //1.创建对象
        Result result = new Result();
        //2.传递数据
        result.setData(data);
        //3.返回对象
        return result;
    }

    /**
     * 操作失败
     */
    public static <T> Result<T> fail(Integer code, String message) {
        //1.创建对象
        Result result = new Result();
        //2.传递状态
        result.setCode(code);
        //3.传递信息
        result.setMessage(message);
        //4.传递数据
        result.setData(null);
        //5.返回对象
        return result;
    }

    /**
     * 操作失败
     * @param businessEnum
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(BusinessEnum businessEnum) {
        //1.创建对象
        Result result = new Result();
        //2.传递状态
        result.setCode(businessEnum.getCode());
        //3.传递信息
        result.setMessage(businessEnum.getDesc());
        //4.传递数据
        result.setData(null);
        //5.返回对象
        return result;
    }
}
